<script>
export default {
    globalData: {
        path: 'pages/index/index',
        goLogin: false,
        queryParams: '',
    },
    onLaunch: function (e) {
        uni.removeStorageSync('inviteQrCode')
        console.log('App->onLaunch:', e)
        if (e.scene) {
            const scene = decodeURIComponent(e.scene)
            console.log('从二维码进入小程序，参数为：', scene)
            if (scene.parent_id) uni.setStorageSync('parent_id', scene.parent_id)
            if (scene.type) uni.setStorageSync('type', scene.type)
        }
        if (e.query) {
            const query = e.query
            console.log('query，参数为：', query)
            if (query.parent_id) uni.setStorageSync('parent_id', query.parent_id)
            if (query.type) uni.setStorageSync('type', query.type)
        }
        // let currentUrl = window.location.href
        // let urlParams = new URLSearchParams(currentUrl.split('?')[1])
        // console.log('urlParams:', urlParams)

        this.checkForUpdate() // 检查新版本
        this._userMe()
        this.getOpenid()
        this.getkefu()
    },
    onShow(e) {
        uni.hideTabBar({
            success: () => {},
            fail: () => {},
        })
    },
    onHide: function () {
        // console.log('App Hide，app不再展现在前台')
    },
    onPageNotFound(e) {
        // 页面不存在 {path: '/1212', query: {a: '123'}, isEntryPage: true}
        console.log('页面不存在:>>>>>>>>>>重新进入首页')
        uni.reLaunch({
            url: '/pages/index/start',
        })
        console.error(e)
    },
    beforeDestroy() {
        // 移除事件监听，释放性能
        uni.$off('$loginOK')
    },
    methods: {
        getOpenid() {
            // #ifdef MP-WEIXIN
            let that = this
            let userInfo = uni.getStorageSync('user')
            wx.login({
                success: function (t) {
                    var o = t.code
                console.log('ooooooooooooo',o)
                    that.$uniapi(
                        '/openid',
                        {
                            code: o,
                        },
                        'POST'
                    ).then((res) => {
                        console.log('res.code：', res.code)
                        if (res.code == 200) {
                            console.log(
                               "2",res.list.session_key,"3",res.tastik ,"4",res.list.openid
                            )
                            // if (res.list.token) uni.setStorageSync('token', res.data.token)
                            if (res.list.session_key) uni.setStorageSync('session_key', res.list.session_key)
                            if (res.list.openid) uni.setStorageSync('openid', res.list.openid)
                            if (res.tastik) uni.setStorageSync('tastik', res.tastik)
                        }
                        if(!userInfo){
                            that._userMe();
                        }
                    })
                },
            })
            // #endif
        },
        async _userMe() {
            try {
                const res = await this.$uniapi('/userInfo')
                if (res.code == 200) {
                    uni.setStorageSync('user', res.list)
                } else if (res.code == 401) {
                    uni.removeStorageSync('user')
                    // uni.removeStorageSync('token')
                } else {
                }
            } catch (err) {}
        },
        async getkefu(){
            try {
                const res = await this.$uniapi('/get_serve_code_list')
                if (res.code == 200) {
                    uni.setStorageSync('kefu', res.list.wechat_code_url)
                }
            } catch (err) {}
        },
        checkForUpdate() {
            // #ifdef MP-WEIXIN
            const updateManager = uni.getUpdateManager()
            if (updateManager) {
                updateManager.onCheckForUpdate(function (res) {
                    // 请求完新版本信息的回调
                    console.log(res.hasUpdate)
                })
                updateManager.onUpdateReady(function (res) {
                    uni.showModal({
                        title: '更新提示',
                        content: '新版本已经准备好，是否重启应用？',
                        success(res) {
                            if (res.confirm) {
                                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                updateManager.applyUpdate()
                            }
                        },
                    })
                })
            }
            // #endif
        },

        async wxmpLogin(code) {
            const res = await this.$uniapi.wechatLogin({
                code,
            })
            if (res.status == 200) {
                let uid = res.data ? res.data.token : null
                let token = res.data ? res.data.token : null
                uni.setStorageSync('uid', uid)
                uni.setStorageSync('token', token)
                uni.setStorageSync('user', res.data)
                if (!res.data.isSined) {
                    console.log('sined page start')
                    uni.redirectTo({
                        url: '/pages/index/sign' + `?${this.globalData.queryParams}`,
                    })
                    return
                } else if (res.data.isSined) {
                    console.log('isSined')
                    uni.reLaunch({
                        url: '/pages/index/index',
                    })
                    return
                }
                setTimeout(() => {
                    uni.$emit('loginOK', {})
                }, 500)
            }
        },
    },
}
</script>

<style lang="scss">
// #ifndef APP-PLUS-NVUE
@import 'style/app.scss';
@import '@/uni_modules/uview-ui/index.scss';
// #endif
</style>
